#coding=utf-8
'''
Created on 2017-09-01

@author: sandy
'''

from twisted.internet.protocol import ReconnectingClientFactory
from twisted.internet import reactor
from pycommon import TBaseProtocol
import logging

class LoginRegisterFactory(ReconnectingClientFactory):
    def __init__(self):
        ReconnectingClientFactory.maxDelay = 5
    
    def buildProtocol(self, addr):
        logging.info(u"login->loginManager CONNECTED!!!")
        self.resetDelay()
        return instance
    
    def startedConnecting(self, connector):
        logging.info(u"login->loginManager connecting!!!")
    
    def clientConnectionLost(self, connector, reason):
        logging.info(u"login->loginManager DOWN!!!")
        ReconnectingClientFactory.clientConnectionLost(self, connector, reason)
        
    def clientConnectionFailed(self, connector, reason):
        logging.info(u"login->loginManager FAIL!!!")
        ReconnectingClientFactory.clientConnectionFailed(self, connector, reason)



class LoginRegister(TBaseProtocol.TBaseProtocol):
    def __init__(self):
        TBaseProtocol.TBaseProtocol.__init__(self)

    def start(self,ip,port):
        reactor.connectTCP(ip,port,LoginRegisterFactory())

    def stop(self):
        logging.warn(u"LoginRegister.stop()")
    
    def connectionMade(self):
        logging.warn(u"LoginRegister.connectionMade()")
        TBaseProtocol.TBaseProtocol.reset(self)

    def connectionLost(self, reason):
        logging.debug(u"LoginRegister.connectionLost %s", reason)

    def packetReceived(self, cmd, pkt):
        try:        
            pass
        except Exception:
            logging.exception(u"LoginRegister.parse() cmd=%d" % cmd)



instance = LoginRegister()

if __name__ == '__main__':
    pass